#include "StdAfx.h"
#include "DXCommon.h"
#include "../sgp/MouseCursorBackground.h"

namespace ja2{ namespace sgp
{
	MouseCursorBackground::MouseCursorBackground(IDirectDraw7 *pDirectDraw)
		: m_Restore(false)
	{
		// Initialize the direct draw surfaces for the mouse background
		DDSURFACEDESC2 surface_description = DDSURFACEDESC2();
		surface_description.dwSize = sizeof(DDSURFACEDESC2);
		surface_description.dwFlags	= DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
		surface_description.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
		surface_description.dwWidth = MAX_CURSOR_WIDTH;
		surface_description.dwHeight = MAX_CURSOR_HEIGHT;
		{
			HRESULT const ret = pDirectDraw->CreateSurface(&surface_description, &m_pSurface, NULL);
			if(ret != DD_OK)
			{
				JA2_DEBUG_MESSAGE(TOPIC_VIDEO, DebugManager::DL_0, resources::FAILED_TO_CREATE_MOUSE_BKGND);
				dx_attempt(ret);
			}
		}
	}

	bool MouseCursorBackground::restore() const
	{
		return m_Restore;
	}

	void MouseCursorBackground::blit(IDirectDrawSurfacePtr &pBuffer)
	{
		RECT region = {m_Left, m_Top, m_Right, m_Bottom};

		HRESULT ret;
		do
		{
			ret = pBuffer->BltFast(m_MouseXPos, m_MouseYPos, m_pSurface, &region, DDBLTFAST_NOCOLORKEY);
				
		}while(ret == DDERR_WASSTILLDRAWING);
		dx_attempt(ret);
	}

	void MouseCursorBackground::update(MouseCursorBackground const &Copy)
	{
		m_Restore = Copy.m_Restore;
		m_MouseXPos = Copy.m_MouseXPos;
		m_MouseYPos = Copy.m_MouseYPos;
		m_Left = Copy.m_Left;
		m_Top = Copy.m_Top;
		m_Right = Copy.m_Right;
		m_Bottom = Copy.m_Bottom;
		m_Region = Copy.m_Region;
	}
} /*sgp*/} /*ja2*/